//成品筛选
var type;
var PlaceOrder=false;//代下单
(function(){
	var inlaysTerm={
		init:function(){
			args = GetUrlParms();				
			if(args["team"]!=undefined){						
				 TEAM = args["team"];//2是现货					
			} 
			if(args["cid"]!=undefined){						
				 Customer_id = args["cid"];	
				 PlaceOrder=true;
			} 
			this.CommonParameters();
			this.bindEvent();
			//this.loadSupplier();
		},
		bindEvent:function(){
			var self=this;
			if(TEAM==2){
				$(".team_params").show();
				$(".normal_params").hide();
			}else{
				$(".team_params").hide();
				$(".normal_params").show();
			}
			// //切换成品类型
			// $("#inlays_type span").on("click",function(){
			// 	$(this).toggleClass("active").siblings().removeClass("active");
			// 	type=$("#inlays_type span.active").attr("data-type");
			// 	self.changType(type);
			// });
			//点击 选中                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
			$("#term_page").on("click",".term span",function(){
				$(this).toggleClass("active");
				if($(this).parent().hasClass("weight")){
					 self.getWeight($(this));
				}
			});
			//点击搜索
			$("#filter_product_btn").on("click",function(){
				indexCount=1
				$("#inlaysettings").html('');
				self.getQueryParameters();
				
			});
			//筛选重置
            $(".reset_term_btn").on("click", function () {
                 $(".term span").removeClass("active");//清除其他条件选中的样式
               $(".term-box input").val("");//清除input框里的值
            });
			 //手动输入重量时移出选中的重量
            $(".weight-input input").keypress(function () {
                $(".weight span").removeClass("active");//移除选中的重量的样式
            });

            //筛选条件
            var ringList = localStorage.getItem("ringList")||'';
            if(ringList){
                self.oldRingSelect(ringList);
            }

		},
		
		//供应商输入提示
		loadSupplier:function(){
			  $.ajax({
		         url: "/cg-api/suppliers-dc/?is_classify=1",//提交地址
		         type: "GET",//提交方式
		         success: function (result) {
		         	
		             if (result.result == true) {
						var availableTags = [];
		             	 for (var i = 0; i < result.rows.length; i++) {
		             	 	var row=result.rows[i];
		             	 	if(row.classify.inlays==true){
		             	 		var supplier_name=row.short_name?(row.short_name):(row.supplier);
		             	 		availableTags.push(supplier_name);
		             	 	}		             	 	
		             	 }
						$("#Supplier").autocomplete({
						 	 source: availableTags
						});
		             }		            		
		         }
		     });
		},
		//公共的参数
		CommonParameters:function(){
			var Type='<h4>'+_("类型")+'</h4>'
			for (var i = 0; i < inlaysetting[1].Type.length; i++) {
				Type += '<span data-value="'+inlaysetting[1].Type[i][0]+'">'+ _(inlaysetting[1].Type[i][1]) +'</span>'
			}

			var Style='<h4>'+_("风格")+'</h4>'
			for (var i = 0; i < inlaysetting[1].Style.length; i++) {
				Style += '<span data-value="'+inlaysetting[1].Style[i][0]+'">'+ _(inlaysetting[1].Style[i][1]) +'</span>'
			}

            var Material='<h4>'+_("材质")+'</h4>'
            for (var i = 0; i < inlaysetting[1].Material.length; i++) {
                Material += '<span style="width:60px" data-value="'+inlaysetting[1].Material[i][0]+'">'+ _(inlaysetting[1].Material[i][1]) +'</span>'
            }

            var Technics='<h4>'+_("镶工")+'</h4>'
            for (var i = 0; i < inlaysetting[1].Technics.length; i++) {
                Technics += '<span data-value="'+inlaysetting[1].Technics[i][0]+'">'+ _(inlaysetting[1].Technics[i][1]) +'</span>'
            }
			$("#Type").html(Type);
			$("#Style").html(Style);
			$("#Material").html(Material);
			$("#Technics").html(Technics);

		},
		// 切换成品类型
		// changType:function(type){
		// 	$("#MainColor,#MainClarity,#Place,#Process").remove();
		// 	//颜色
		// 	if(type && type!=1){
		// 		var MainColor='<div data-name="MainColor" id="MainColor" class="term"><h4>'+_("颜色")+'</h4>'
		// 		for (var i = 0; i < inlay[type].MainColor.length; i++) {
		// 			MainColor += '<span data-value="'+inlay[type].MainColor[i][0]+'">'+ _(inlay[type].MainColor[i][1]) +'</span>'
		// 		}
		// 		MainColor+='</div>'
		// 		$("#Weight").after(MainColor);
		// 		if(type==2 || type==3){
		// 			var MainClarity='<div data-name="MainClarity" id="MainClarity" class="term"><h4>'+_("净度")+'</h4>'
		// 			for(var i=0;i<inlay[type].MainClarity.length;i++){
		// 				MainClarity+='<span data-value="'+inlay[type].MainClarity[i][0]+'">'+inlay[type].MainClarity[i][1]+'</span>'
		// 			}
		// 			MainClarity+='</div>'
		// 			$("#MainColor").after(MainClarity);
		// 		}
		// 		if(type>3){
		// 			var Place='<div data-name="Place" id="Place" class="term"><h4>'+_("产地")+'</h4>'
		// 			for(var i=0;i<inlay[type].Place.length;i++){
		// 				Place+='<span data-value="'+inlay[type].Place[i][0]+'">'+_(inlay[type].Place[i][1])+'</span>'
		// 			}
		// 			Place+='</div>'
		// 			$("#Price").after(Place);
		// 			if(type==3 || type==4){
		// 				var Process='<div data-name="Process" id="Process" class="term"><h4>'+_("处理")+'</h4>'
		// 				for(var i=0;i<inlay[type].Process.length;i++){
		// 					Process+='<span data-value="'+inlay[type].Process[i][0]+'">'+_(inlay[type].Process[i][1])+'</span>'
		// 				}
		// 				Process+='</div>'
		// 				$("#Place").after(Process);
		// 			}
		// 		}
		// 	}
		// },
		//获取重量值
        getWeight: function ($this) {
          var weightSelected = $(".weight span.active");//选中的重量
            if (weightSelected.length == 1) {//如果只选择一个
                $("#weight-min").val(weightSelected.attr("data-value").split("-")[0]);
                $("#weight-max").val(weightSelected.attr("data-value").split("-")[1]);
            }
            else if (weightSelected.length > 1) {//选择多个
                var min = weightSelected[0];//选中的序列中的第一个的值
                var max = weightSelected[weightSelected.length - 1];//选中的序列中的最后一个的值
                $("#weight-min").val($(min).attr("data-value").split("-")[0]);//绑值
                $("#weight-max").val($(max).attr("data-value").split("-")[1]);
            } else {
                $("#weight-min").val("");
                $("#weight-max").val("");
            }
        },
		//获取参数
        getQueryParameters: function () {
           var params = {};
            params.limit = "500";
            params.useDict = true;
            params.GoodsType=3;
            if(TEAM==2){
                params.IsMyStock=true;//现货
			}else{
            	params.OnShelves=1;
			}
            isSearching = true;
            var pageSize = 1;//500
            //查询开始时间
            var dtSearchStart = new Date().getTime();
            //循环找出筛选框里选中的参数
            $(".inlays-term .term").each(function (i, item) {
        		var field = $(item).attr("data-name");
		        var dataType = $(item).attr("data-type");
		        if (field) {
		            var values = [];
		            $(item).find("span.active").each(function (j, p) {
		                var value = $(p).attr("data-value");
		                value = value ? value : $(p).text();
		                value = dataType == 'range' ? value.split('-') : value;		                
		                values.push(value);
		            });
		
		            if (values.length > 0) {
		                if (dataType == 'range') {
		                    if (!params.between) {
		                        params.between = {};
		                    }
		                    params.between[field] = values;
		                } else {
		                    params[field] = values;
		                }
		            }
		        }              
           });
            //输入重量
		    var select_weight=$(".weight span").hasClass("active");
		    var min=$("#weight-min").val();
		    var max=$("#weight-max").val();
		    if(!select_weight && min!="" && max!=""){
		    	params.between = {};
		    	params.between.MainWeight=[min,max];
		    }
		    else if(!select_weight && min!="" && max==""){
		    	params.between = {};
		    	params.between.MainWeight=[min,99.99];
		    }
		    else if(!select_weight && min=="" && max!=""){
		    	params.between = {};
		    	params.between.MainWeight=[0,max];
		    }
		     else if(!select_weight && min=="" && max==""){
		    }

		    var OriginSN=$("#OriginSN").val();
		    if(OriginSN!=""){
		    	params.OriginSN=OriginSN;
		    }
            var manufactor = $("#Manufactor").val();
            if (manufactor!="") {
                params.Manufactor = manufactor;
            }
            var manufactor = $("#Manufactor").val();
            if (manufactor!="") {
                params.Manufactor = manufactor;
            }

			var regs = /^[a-zA-Z]+[0-9]+|^[0-9]+[a-zA-Z]+|^[0-9]+|^[a-zA-Z]+/;
			var search_StyleNo = $("#StyleNo").val();
			if (search_StyleNo != "") {
				params.startsWith={}
				params.startsWith.StyleNo = search_StyleNo.match(regs)[0];
			}

            var series = $("#Series").val();
            if (series!="") {
                params.Series = series;
            }
		    // params.Category=type;
			if(TEAM==0){
				var url_href="/Customer/InlaySettings?team=0";//客户端的空托页面
			}else if(TEAM==1){
				var url_href="/Team/InlaySettings?team=1";//团队端的空托页面
			}else if(TEAM==2){
				var url_href="/Team/MyInlaySettings?team=2&type=7";//团队端的现货空托页面
			}else if(TEAM==3){
				var url_href="/Team/InlaySettings?team=3&cid="+Customer_id+'&userName='+NullFormat(getUrlParam('userName'));//团队端的代下单
			}
			var action=url_href;
		  	form = $("<form method='post' id='params_form' action='"+action+"'>"+
		   	"<input type='text' value='"+JSON.stringify(params)+"' name='params'></form>")
			$("#form_box").html(form);
			$("#params_form").submit();

			//成品货品查找筛选
				localStorage.setItem('ringList',JSON.stringify(params));

        },

		//恢复成品筛选条件start
        oldRingSelect:function(data){
			var self=this;
            var oldData=JSON.parse(data);

			// var category=oldData.Category;
            // self.changType(category)

            for(var name in oldData){//遍历对象属性名


				//公共部分start
				if(name=='Category'){ //主石类型
					$("#inlays_type").find('span[data-type="'+oldData[name]+'"]').addClass('active');
					type=oldData[name];
				}
                if(name=='MainShape' ){//形状
                    for(var i=0;i<oldData[name].length;i++){
                        $('#term_page').find('div[data-name="'+name+'"]').find('span[data-value="'+oldData[name][i]+'"]').addClass('active')
                    }
                }
                if(name=='Type' ){//类型
                    for(var i=0;i<oldData[name].length;i++){
                        $('#term_page').find('div[data-name="'+name+'"]').find('span[data-value="'+oldData[name][i]+'"]').addClass('active')
                    }
                }

                if(name=='Style' ){//风格款式
                    for(var i=0;i<oldData[name].length;i++){
                        $('#term_page').find('div[data-name="'+name+'"]').find('span[data-value="'+oldData[name][i]+'"]').addClass('active')
                    }
                }

                if(name=='IsStock'){ //现货 定制
                    for(var i=0;i<oldData[name].length;i++){
                        $('#term_page').find('div[data-name="'+name+'"]').find('span[data-value="'+oldData[name][i]+'"]').addClass('active')
                    }
                }


                if(name=='OriginSN'){ //货号
                	$("#OriginSN").val(oldData.OriginSN);
				}

                if(name=='shortName'){ //供应商
                    $("#Supplier").val(oldData.shortName);
                }

				if(name=='OnShelves'){ //上下架
                    for(var i=0;i<oldData[name].length;i++){
                        $('#term_page').find('div[data-name="'+name+'"]').find('span[data-value="'+oldData[name][i]+'"]').addClass('active')
                    }
				}

				if(name=='between'){ //主石重量
                    if(oldData[name].MainWeight){
                    	var len=oldData[name].MainWeight.length;
                    	$('#weight-min').val(oldData[name].MainWeight[0][0]);
                    	$('#weight-max').val(oldData[name].MainWeight[len-1][1]);
                        for(var i=0;i<oldData[name].MainWeight.length;i++){
                        	var mainight=oldData[name].MainWeight[i][0]+'-'+oldData[name].MainWeight[i][1];
                            $('#term_page').find('div[data-name="MainWeight"]').find('span[data-value="'+mainight+'"]').addClass('active')
                        }
                    }
                    if(oldData[name].Price){ //价格
                        for(var i=0;i<oldData[name].Price.length;i++){
                            var price=oldData[name].Price[i][0]+'-'+oldData[name].Price[i][1];
                            $('#term_page').find('div[data-name="Price"]').find('span[data-value="'+price+'"]').addClass('active')
                        }
                    }
				}
                //公共部分end


                if(name=='MainColor'){ //颜色
                    for(var i=0;i<oldData[name].length;i++){
                        $('body #term_page').find('div[data-name="'+name+'"]').find('span[data-value="'+oldData[name][i]+'"]').addClass('active')
                    }
                }

				if(name=='MainClarity'){ //净度
                    for(var i=0;i<oldData[name].length;i++){
                        $('body #term_page').find('div[data-name="'+name+'"]').find('span[data-value="'+oldData[name][i]+'"]').addClass('active')
                    }
				}
                if(name=='Place'){ //产地
                    for(var i=0;i<oldData[name].length;i++){
                        $('#term_page').find('div[data-name="'+name+'"]').find('span[data-value="'+oldData[name][i]+'"]').addClass('active')
                    }
                }


            }
		},
	}
	inlaysTerm.init();
})();
